<?php

/**
 * Виджет BoardWidget
 * 
 * @author Kosty Ryabichenko <k.ryabichenko@gmail.com>
 * @version 1.0
 */
class BoardWidget extends CWidget {

    public function init() {

        $page = Yii::app()->params['page'];
        $search = Yii::app()->params['searchParams'];

        if ($page->url == '/') {
            
            $criteria = new CDbCriteria;
            $criteria->addCondition("t.is_active = 1");
            $criteria->with = array('сity' => array('condition' => 'сity.is_active = 1'));
            $criteria->limit = '16';
            $criteria->offset = '0';
            $criteria->order = 't.id DESC';
            $flats = Flat::model()->findAll($criteria);
            
            $this->render('board', array(
                'flats' => $flats,
            ));
        }
        
        if ($page->status == Page::model()->getTypePage('city')) {

            $city = City::model()->getCityFromUrl($page->url);
            
            $criteria = new CDbCriteria;
            
            $criteria->addCondition("t.is_active = 1");
            
            if(!empty($search['type_id'])){
                $criteria->addCondition("type_id = ".$search['type_id']."");
            }
            
            if(!empty($search['guests'])){
                $criteria->addCondition("guests = ".$search['guests']."");
            }
                        
            $from = !empty($search['inputPriceFrom']) ? $search['inputPriceFrom'] : Flat::getPriceFromCity($search['city_id'], 'min');
            $to = !empty($search['inputPriceTo']) ? $search['inputPriceTo'] : Flat::getPriceFromCity($search['city_id'], 'max');            
            $criteria->addBetweenCondition("price", $from, $to);
            
            $criteria->with = array('сity' => array(
                    'condition' => 'url = :url AND сity.is_active = 1',
                    'params' => array(':url' => $page->url),
            ));
            $criteria->order = 't.id DESC';
            $flats = Flat::model()->findAll($criteria);
            
            $this->render('board', array(
                'flats' => $flats,
                'city' => $city,
            ));
        }
    }

    public function run() {
        // этот метод будет вызван внутри CBaseController::endWidget()
    }

}

?>
